import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 14229 on 2020/2/24.
 */
public class Test09Excel {
    public static void main(String[] args) throws IOException {
        List<List<String>>  list = new ArrayList<>();
        // 获取Excel表
        InputStream is = new FileInputStream(new File("a.xlsx"));
        XSSFWorkbook swb = new XSSFWorkbook(is);
        //  先从每页开始循环
        for (Sheet rows : swb) {
            // 判断如果是空页 就跳出本次循环 进行下一页循环
            if (rows==null){
                continue;
            }
            // 处理当前页 开始循环读取每一行  getLastRowNum获取最后一行
            for (int i = 0; i <=rows.getLastRowNum() ; i++) {
                // 当前光标所在行的下标
                Row r = rows.getRow(i);

                int max = r.getLastCellNum();
                int min = r.getFirstCellNum();
                List<String> rowlist = new ArrayList<>();
                //遍历该行 获取每个cell元素
                for (int j = min; j <max ; j++) {
                    Cell c = r.getCell(j);
                    if (c==null){
                        continue;
                    }
                    rowlist.add(c.toString());
                }
                list.add(rowlist);
            }
        }
        for (List<String> strings : list) {
            System.out.println(strings);
        }
        is.close();
    }
}
